//
//  ViewController.m
//  08-Homework
//
//  Created by dllo on 16/7/25.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "ViewController.h"
#import "Model.h"
#import "DetailsViewController.h"
#import "AddViewController.h"
#define RESULT @"item"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate,AddViewControllerDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *arr;
@property (nonatomic, strong)UILabel *label;
@property (nonatomic, strong)NSMutableArray *selectName;
@property (nonatomic, strong)NSMutableArray<NSIndexPath *> *selectIndexArr;
@end

@implementation ViewController
- (NSMutableArray *)arr {
    if (!_arr) {
       self.arr = [NSMutableArray array];
    }
    return _arr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(clickLeftButton:)];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.view.backgroundColor = [UIColor whiteColor];
   NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160710(1)" ofType:@"plist"];
    NSArray *arrPlist = [NSArray arrayWithContentsOfFile:path];
    for (NSDictionary *dic in arrPlist) {
        Model *model = [Model modelWithDic:dic];
        [self.arr addObject:model];
    }
    NSLog(@"%ld", _arr.count);
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:RESULT];
    self.selectIndexArr = [NSMutableArray array];
    self.selectName = [NSMutableArray array];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:RESULT forIndexPath:indexPath];
    Model *model = _arr[indexPath.row];
    cell.textLabel.text = model.name;
    
    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    DetailsViewController *details = [[DetailsViewController alloc] init];
    details.model = _arr[indexPath.row];
    [self.navigationController pushViewController:details animated:YES];
    //[_tableView reloadData];
}
- (void)clickLeftButton:(UIBarButtonItem *)leftBtn {
    AddViewController *add = [[AddViewController alloc] init];
    add.delegate = self;
    [self.navigationController pushViewController:add animated:YES];
}
- (void)addModel:(Model *)model {
    [_arr addObject:model];
    [_tableView reloadData];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 1.修改数据源
        [_arr removeObjectAtIndex:indexPath.row];
        //对应修改数据
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        [_arr insertObject:@"@.@" atIndex:indexPath.row];
        [_tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }

}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
    if (editing) {
        NSLog(@"编辑");
    } else {
        // 删除数据源
        [_arr removeObjectsInArray:_selectName];
        // 要删除选中的数组
        [_tableView deleteRowsAtIndexPaths:_selectIndexArr withRowAnimation:UITableViewRowAnimationLeft];
        [_selectName removeAllObjects];
        [_selectIndexArr removeAllObjects];
        NSLog(@"完成");
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
